<?php
/**
 * 登录
 *
 * 包括 登录 验证 退出 操作
 *
 *
 * @好商城提供技术支持 授权请购买shopnc授权
 * @license    http://www.33hao.com
 * @link       交流群号：138182377
 */



defined('In33hao') or exit('Access Invalid!');
class LoginControl extends SystemControl {

    /**
     * 不进行父类的登录验证，所以增加构造方法重写了父类的构造方法
     */
    public function __construct(){
//        Language::read('common,layout,login');
//        $result = chksubmit(true,true,'num');
//        if ($result){
//            if ($result === -11){
//                showMessage('非法请求');
//            }elseif ($result === -12){
//                showMessage(L('login_index_checkcode_wrong'));
//            }
//            if (process::islock('admin')) {
//                showMessage('您的操作过于频繁，请稍后再试');
//            }
//            $obj_validate = new Validate();
//            $obj_validate->validateparam = array(
//                array("input"=>$_POST["user_name"], "require"=>"true", "message"=>L('login_index_username_null')),
//                array("input"=>$_POST["password"],  "require"=>"true", "message"=>L('login_index_password_null')),
//                array("input"=>$_POST["captcha"],   "require"=>"true", "message"=>L('login_index_checkcode_null')),
//            );
//            $error = $obj_validate->validate();
//            if ($error != '') {
//                showMessage(L('error').$error);
//            } else {
//                $model_admin = Model('admin');
//                $array  = array();
//                $array['admin_name']    = $_POST['user_name'];
//                $array['admin_password']= md5(trim($_POST['password']));
//                $admin_info = $model_admin->infoAdmin($array);
//                if(is_array($admin_info) and !empty($admin_info)) {
//                    if ($admin_info['admin_gid'] > 0) {
//                        $gamdin_info = Model('gadmin')->getGadminInfoById($admin_info['admin_gid']);
//                        $group_name = $gamdin_info['gname'];
//                    } else {
//                        $group_name = '超级管理员';
//                    }
//                    $array = array();
//                    $array['name']  = $admin_info['admin_name'];
//                    $array['id']    = $admin_info['admin_id'];
//                    $array['time']  = $admin_info['admin_login_time'];
//                    $array['ip']    = getIp();
//                    $array['gid']   = $admin_info['admin_gid'];
//                    $array['gname'] = $group_name;
//                    $array['sp']    = $admin_info['admin_is_super'];
//                    $array['qlink'] = $admin_info['admin_quick_link'];
//                    $this->systemSetKey($array, $admin_info['admin_avatar'], true);
//                    $update_info    = array(
//                        'admin_id'=>$admin_info['admin_id'],
//                        'admin_login_num'=>($admin_info['admin_login_num']+1),
//                        'admin_login_time'=>TIMESTAMP
//                    );
//                    $model_admin->updateAdmin($update_info);
//                    $this->log(L('nc_login'),1);
//                    process::clear('admin');
//                    @header('Location: index.php');exit;
//                } else {
//                    process::addprocess('admin');
//                    showMessage(L('login_index_username_password_wrong'),'index.php?act=login&op=login');
//                }
//            }
//        }
//        Tpl::output('html_title',L('login_index_need_login'));
//        Tpl::showpage('login','login_layout');
    }

    public function oldOp(){
        Language::read('common,layout,login');
        $result = chksubmit(true,true,'num');
        if ($result){
            if ($result === -11){
                showMessage('非法请求');
            }elseif ($result === -12){
                showMessage(L('login_index_checkcode_wrong'));
            }
            if (process::islock('admin')) {
                showMessage('您的操作过于频繁，请稍后再试');
            }
            $obj_validate = new Validate();
            $obj_validate->validateparam = array(
                array("input"=>$_POST["user_name"], "require"=>"true", "message"=>L('login_index_username_null')),
                array("input"=>$_POST["password"],  "require"=>"true", "message"=>L('login_index_password_null')),
                array("input"=>$_POST["captcha"],   "require"=>"true", "message"=>L('login_index_checkcode_null')),
            );
            $error = $obj_validate->validate();
            if ($error != '') {
                showMessage(L('error').$error);
            } else {
                $model_admin = Model('admin');
                $array  = array();
                $array['admin_name']    = $_POST['user_name'];
                $array['admin_password']= md5(trim($_POST['password']));
                $admin_info = $model_admin->infoAdmin($array);
                if(is_array($admin_info) and !empty($admin_info)) {
                    if ($admin_info['admin_gid'] > 0) {
                        $gamdin_info = Model('gadmin')->getGadminInfoById($admin_info['admin_gid']);
                        $group_name = $gamdin_info['gname'];
                    } else {
                        $group_name = '超级管理员';
                    }
                    $array = array();
                    $array['name']  = $admin_info['admin_name'];
                    $array['id']    = $admin_info['admin_id'];
                    $array['time']  = $admin_info['admin_login_time'];
                    $array['ip']    = getIp();
                    $array['gid']   = $admin_info['admin_gid'];
                    $array['gname'] = $group_name;
                    $array['sp']    = $admin_info['admin_is_super'];
                    $array['qlink'] = $admin_info['admin_quick_link'];
                    $this->systemSetKey($array, $admin_info['admin_avatar'], true);
                    $update_info    = array(
                        'admin_id'=>$admin_info['admin_id'],
                        'admin_login_num'=>($admin_info['admin_login_num']+1),
                        'admin_login_time'=>TIMESTAMP
                    );
                    $model_admin->updateAdmin($update_info);
                    $this->log(L('nc_login'),1);
                    process::clear('admin');
                    @header('Location: index.php');exit;
                } else {
                    process::addprocess('admin');
                    showMessage(L('login_index_username_password_wrong'),'index.php?act=login&op=login');
                }
            }
        }
        Tpl::output('html_title',L('login_index_need_login'));
        Tpl::showpage('old_login','login_layout');
    }

    /**
     * 登录验证
     */
    public function checkLoginOp(){
        Language::read('common,layout,login');
        $result = chksubmit(true,false,'num');
        if ($result){
            if ($result === -11){
                showMessage('非法请求');
            }elseif ($result === -12){
                showMessage(L('login_index_checkcode_wrong'));
            }
            if (process::islock('admin')) {
                showMessage('您的操作过于频繁，请稍后再试');
            }
            $obj_validate = new Validate();
            $obj_validate->validateparam = array(
                array("input"=>$_POST["user_name"], "require"=>"true", "message"=>L('login_index_username_null')),
                array("input"=>$_POST["auth_code"],  "require"=>"true", "message"=>"动态验证码不能为空"),
            );
            $error = $obj_validate->validate();
            if ($error != '') {
                showMessage(L('error').$error);
            } else {
                // 验证短信动态码
                if (!$_POST['auth_code'] || !preg_match('/^\d{6}$/', $_POST['auth_code'])) {
                    showMessage('验证码错误');
                }
                $model_member = Model('admin');

                $member_common_info = $model_member->getMemberCommonInfo(array('admin_id' => $_POST['admin_id']));
                if (empty($member_common_info) || !is_array($member_common_info)) {
                    showMessage('验证失败');
                }
                if (TIMESTAMP - $member_common_info['send_acode_time'] > 1800) {
                    showMessage('验证码已失效，请重新获取');
                }
                if ($member_common_info['auth_code_check_times'] > 3) {
                    showMessage('输入错误次数过多，请重新获取');
                }
                if ($_POST['auth_code'] != $member_common_info['auth_code']) {
                    $update_data['auth_code_check_times'] = array('exp', 'auth_code_check_times+1');
                    $update = $model_member->editMemberCommon($update_data, array('admin_id' => $_POST['admin_id']));
                    showMessage('短信验证码错误，请重新获取');
                }
                $model_admin = Model('admin');
                $array  = array();
                $array['mobile']    = $_POST['user_name'];
                $admin_info = $model_admin->infoAdmin($array);
                if(is_array($admin_info) and !empty($admin_info)) {
                    if ($admin_info['admin_gid'] > 0) {
                        $gamdin_info = Model('gadmin')->getGadminInfoById($admin_info['admin_gid']);
                        $group_name = $gamdin_info['gname'];
                    } else {
                        $group_name = '超级管理员';
                    }
                    $array = array();
                    $array['name']  = $admin_info['admin_name'];
                    $array['id']    = $admin_info['admin_id'];
                    $array['time']  = $admin_info['admin_login_time'];
                    $array['ip']    = getIp();
                    $array['gid']   = $admin_info['admin_gid'];
                    $array['gname'] = $group_name;
                    $array['sp']    = $admin_info['admin_is_super'];
                    $array['qlink'] = $admin_info['admin_quick_link'];

                    $this->systemAdminSetKey($array, $admin_info['admin_avatar'], true);
                    $update_info    = array(
                        'admin_id'=>$admin_info['admin_id'],
                        'admin_login_num'=>($admin_info['admin_login_num']+1),
                        'admin_login_time'=>TIMESTAMP
                    );
                    $model_admin->updateAdmin($update_info);
                    $this->log(L('nc_login'),1);
                    process::clear('admin');
                    @header('Location: index.php');exit;
                } else {
                    process::addprocess('admin');
                    showMessage(L('login_index_username_password_wrong'),'index.php?act=login&op=login');
                }
            }
        }
    }

    /**
     * 发送短信验证码
     */
    public function sendsmscodeOp(){
        $_mobile = $_POST['mobile'] ;
        // 检测管理员手机号码是否存在
        $where['mobile'] = $_mobile;
        $model_admin = Model('admin');
        $admin_info = $model_admin->infoAdmin($where);
        if(empty($admin_info)){
            $res = ['status'=>400,'msg'=>"手机号错误"];
            echo json_encode($res);
            exit();
        }
//         发送验证码
          $logic_connect_api = Logic('connect_api');
          $result = $logic_connect_api->sendAdminCaptcha($_mobile, 2);
          if($result['state']){
              $res = ['status'=>200,'msg'=>"短信验证码发送成功",'admin_id'=>$admin_info['admin_id']];
          }else{
              $res = ['status'=>400,'msg'=>$result['msg']];
          }
//        $res = ['status'=>200,'msg'=>"短信验证码发送成功",'admin_id'=>$admin_info['admin_id']];
        echo json_encode($res);
    }

    /**
     * ajax 验证验证码是否正确
     */
    public function checkcaptchaOp(){
        if (!checkSeccode($_POST['nchash'],$_POST['captcha'])){
             echo 'false';
        }else{
             echo 'true';
        }
    }

    public function loginOp(){
        Language::read('common,layout,login');
        Tpl::output('html_title',L('login_index_need_login'));
        Tpl::showpage('login','login_layout');
    }

    public function indexOp(){
        Language::read('common,layout,login');
        Tpl::output('html_title',L('login_index_need_login'));
        Tpl::showpage('login','login_layout');
    }
}
